package com.sywb.chuangyebao.view;

import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.contract.SearchContract;

import org.bining.footstone.log.Logger;
import org.bining.footstone.utils.ToastUtils;
import org.bining.footstone.widget.DrawableEditText;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 搜索
 */
public class SearchActivity extends ActionbarActivity<SearchContract.Presenter> implements SearchContract.View {

    @BindView(R.id.srarch_edit)
    DrawableEditText srarchEdit;

    @BindView(R.id.common_recycler_layout)
    RelativeLayout commonRecyclerLayout;
    @BindView(R.id.common_recycler)
    RecyclerView commonRecycler;
    @BindView(R.id.common_nodata)
    RelativeLayout commonNodata;
    @BindView(R.id.common_nodata_content)
    TextView commonNodataContent;
    @BindView(R.id.common_nodata_icon)
    ImageView commonNodataIcon;
    @BindView(R.id.common_nodata_subtitle)
    TextView commonNodataSubtitle;

    @BindView(R.id.search_result)
    LinearLayout searchResult;
    @BindView(R.id.search_channel)
    TabLayout searchChannel;
    @BindView(R.id.search_viewpager)
    ViewPager searchViewpager;

    private String pushKey;

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.activity_search;
    }

    @Override
    public void initView(Bundle bundle) {
        super.initView(bundle);
        if (bundle == null) {
            pushKey = getIntent().getStringExtra("p0");
        } else {
            pushKey = bundle.getString("p0");
        }

        srarchEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence == null || charSequence.length() == 0) {
                    commonRecyclerLayout.setVisibility(View.VISIBLE);
                    searchResult.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });

        srarchEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    String keyword = srarchEdit.getText().toString();
                    startSearch(keyword);
                }
                return false;
            }
        });
    }

    @Override
    public void initPresenter() {
        super.initPresenter();
        if (!TextUtils.isEmpty(pushKey)) {
            commonRecyclerLayout.setVisibility(View.GONE);
            searchResult.setVisibility(View.VISIBLE);
            mPresenter.addLoacSearch(pushKey);
            srarchEdit.setText(pushKey);
            // startSearch(pushKey);
        }
    }

    @OnClick({R.id.srarch_back, R.id.search_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.srarch_back:
                exit();
                break;
            case R.id.search_button:
                String keyword = srarchEdit.getText().toString();
                startSearch(keyword);
                break;
        }
    }

    @Override
    public boolean useRxBus() {
        return true;
    }

    @Override
    public boolean useLightMode() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
    }

    @Override
    public RecyclerView getRecyclerView() {
        return commonRecycler;
    }

    @Override
    public void setNoData(boolean isNoData) {
    }

    @Override
    public FragmentManager getUserFragmentManager() {
        return getSupportFragmentManager();
    }

    @Override
    public TabLayout getTabLayout() {
        return searchChannel;
    }

    @Override
    public ViewPager getViewPager() {
        return searchViewpager;
    }

    @Override
    public String getPushKey() {
        return pushKey;
    }

    @Override
    public void startSearch(String keyword) {
        Logger.e("startSearch :" + keyword);
        if (TextUtils.isEmpty(keyword)) {
            ToastUtils.show(mContext, "请输入搜索内容");
        } else {
            commonRecyclerLayout.setVisibility(View.GONE);
            searchResult.setVisibility(View.VISIBLE);
            mPresenter.addLoacSearch(keyword);
            srarchEdit.setText(keyword);

            mPresenter.startSearch(keyword);
        }
    }
}
